#!/usr/bin/env ruby

# Author: Greg Mefford
# This script can be used to split the 13-channel RAW file into 13 PNG files for editing.

require 'RMagick'
include Magick

RAWFile = "Sim.raw"
OutputPrefix = "channel"

channels = ImageList.new
13.times do
  channels << Image.new(256, 256) {
    self.background_color = 'black'
  }
end
data = []
File.open(RAWFile,"rb").each_byte { |b| data << b }
max_index = data.length

width = height = 256
(0..height).each do |y|
  (0..width).each do |x|
    (0..12).each do |channel|
      index = y * (13 * width) + x * (13) + channel
      channels[channel].import_pixels(x, y, 1, 1, "I", data[index]) if index < max_index
    end
  end
end

counter = 0
channels.each do |channel|
  channel.write("#{OutputPrefix}#{counter += 1}.png")
end